import { asyncRoutes, constantRoutes } from '@/router';
import { filterAsyncRoutes } from '@/utils/handleRoutes';

const state = () => ({
  routes: [],
  routeFlag: false,
});

const getters = {
  routes: (state) => state.routes,
  routeFlag: (state) => state.routeFlag,
};

const mutations = {
  setRoutes(state, routes) {
    state.routes = constantRoutes.concat(routes);
  },
  setRouteFlag(state, flag) {
    state.routeFlag = flag;
  },
  clearRoutes(state) {
    state.routes = [];
  },
};

const actions = {
  async setRoutes({ commit }, permissions) {
    const finallyAsyncRoutes = await filterAsyncRoutes([...asyncRoutes], permissions);
    commit('setRoutes', finallyAsyncRoutes);
    return finallyAsyncRoutes;
  },
  setRouteFlag({ commit }, flag) {
    commit('setRouteFlag', flag);
  },
  clearRoutes({ commit }) {
    commit('clearRoutes');
  },
};

export default { state, getters, mutations, actions };
